package cn.tansanqinger.controller.menu;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import cn.tansanqinger.pojo.Menu;
import cn.tansanqinger.service.IMenuService;
import cn.tansanqinger.util.Encoder_Dispose;
import cn.tansanqinger.util.PageData;
import cn.tansanqinger.util.Tools;
import net.sf.json.JSONArray;

@Controller
@RequestMapping("/menu")
public class MenuController {
	
	@Autowired
	private IMenuService menuService;
	
	/**
	 * 菜单编辑的首页
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/index")
	public ModelAndView index(ModelAndView model) throws Exception {
		model.addObject("menuList", menuService.selectFirstMenuAll());
		model.setViewName("menu/index");
		return model;
	}
	
	/**
	 * 请求新增菜单页面
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/toAdd")
	public ModelAndView toAdd(ModelAndView mv)throws Exception{
		try{
			mv.addObject("menuList", menuService.selectFirstMenuAll());
			mv.setViewName("/menu/menu_add");
		} catch(Exception e){
		}
		return mv;
	}
	
	/**
	 * 保存菜单信息
	 * @param menu
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/add")
	public ModelAndView add(HttpServletRequest request, ModelAndView mv)throws Exception{
		try{
			Menu menu = new Menu();
			String menuId = request.getParameter("menuId");
			if("0".equals(Tools.isEmpty(request.getParameter("parentId")))) {
				int count = menuService.selectIdCount(0);
				System.err.println(count);
				if(count==0) {
					menuId = "0";
				} else {
					menuId = count+ 10 +"";
				}
			} else {
				//menuId = menuService.selectIdCount(Integer.parseInt(Tools.isEmpty(request.getParameter("parentId"))));
			}
			menu.setMenuId(Integer.parseInt(menuId));
			menu.setMenuName(request.getParameter("menuName"));
			menu.setParentId(request.getParameter("parentId"));
			menu.setMenuType(request.getParameter("menuType"));
			menu.setMenuUrl(request.getParameter("menuUrl"));
			menu.setMenuOrder(request.getParameter("menuOrder"));
			menuService.saveMenu(menu);
			mv.addObject("msg","success");
		} catch(Exception e){
			mv.addObject("msg","failed");
		}
		
		mv.setViewName("save_result");
		return mv;
		
	}
	
	/**
	 * 请求编辑菜单页面
	 * @param 
	 * @return
	 */
	@RequestMapping(value="/toEdit")
	public ModelAndView toEdit(@RequestParam String menuId,ModelAndView mv)throws Exception{
		try{
			mv.addObject("menuId",menuId);
			mv.addObject("menuList", menuService.selectFirstMenuAll());
			mv.addObject("menu", menuService.getMenuById(Integer.parseInt(menuId)));
			mv.setViewName("/menu/menu_edit");
		} catch(Exception e){
		}
		return mv;
	}
	
	/**
	 * 请求编辑菜单图标页面
	 * @param 
	 * @return
	 */
	@RequestMapping(value="/toEditicon")
	public ModelAndView toEditicon(@RequestParam String menuId,ModelAndView mv)throws Exception{
		mv.addObject("menuId",menuId);
		mv.setViewName("menu/menu_icon");
		return mv;
	}
	
	/**
	 * 保存菜单图标 (顶部菜单)
	 * @param 
	 * @return
	 */
	@RequestMapping(value="/editicon")
	public ModelAndView editicon(ModelAndView mv, HttpServletRequest request)throws Exception{
		try{
			String menuId = request.getParameter("menuId");
			String menuIcon = request.getParameter("menuIcon");
			if(Encoder_Dispose.isNull(menuIcon,menuId)) {
				if(menuId.matches("\\d+")) {
					menuService.updataMenuIcon(Integer.parseInt(menuId), menuIcon);
					mv.addObject("msg","success");
				}
			}
			mv.addObject("msg","error");
		} catch(Exception e){
			mv.addObject("msg","failed");
		}
		mv.setViewName("save_result");
		return mv;
	}
	
	/**
	 * 保存编辑
	 * @param 
	 * @return
	 */
	@RequestMapping(value="/edit")
	public ModelAndView edit(ModelAndView mv, HttpServletRequest request)throws Exception{
		try{
			Menu menu = new Menu();
			String menuId = request.getParameter("menuId");
			if(menuId.matches("\\d+")) {
				menu.setMenuId(Integer.parseInt(menuId));
			}
			menu.setMenuName(request.getParameter("menuName"));
			menu.setParentId(request.getParameter("parentId"));
			menu.setMenuType(request.getParameter("menuType"));
			menu.setMenuUrl(request.getParameter("menuUrl"));
			menu.setMenuOrder(request.getParameter("menuOrder"));
			menuService.updataMenuAll(menu);
			mv.addObject("msg","success");
		} catch(Exception e){
			mv.addObject("msg","failed");
		}
		mv.setViewName("save_result");
		return mv;
	}
	
	/**
	 * 一级菜单展开
	 * @param MENU_ID
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/sub")
	public void getSub(@RequestParam String menuId,HttpServletResponse response)throws Exception{
		List<Menu> subMenu = menuService.selectNextMenuAll(menuId);
		JSONArray arr = JSONArray.fromObject(subMenu);
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		String json = arr.toString();
		out.write(json);
		out.flush();
		out.close();
	}
	
	/**
	 * 删除菜单
	 * @param menuId
	 * @param out
	 */
	@RequestMapping(value="/del")
	public void delete(@RequestParam String menuId,PrintWriter out)throws Exception{
		if(menuId.matches("\\d+")) {
			menuService.deleteMenuById(menuId);
			out.write("success");
		} else {
			out.write("error");
		}
		out.flush();
		out.close();
	}
	
}
